package updater;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;


import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.awt.Font;
import java.awt.Color;
import javax.swing.JProgressBar;

public class Gui extends javax.swing.JFrame{

	private JFrame frmWotModsUpdater;
	public JProgressBar progressBar = new JProgressBar();
	private JTextPane txtpn;
	
	
	

	/**
	 * Launch the application.
     * @param args
	 */
	public static void main(String[] args){
		SwingUtilities.invokeLater(new Runnable() {
                        @Override
			public void run() {
				
					Gui window;
					try {
						window = new Gui();
						window.frmWotModsUpdater.setVisible(true);
					} catch (Exception e) {
					}
					
								}
			
		});
		
	}

	/**
	 * Create the application. 
	 */
	public Gui() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 * @throws Exception 
	 */
	private void initialize() {
		
		String serverVersion = "Lesefehler";
		try {
			serverVersion = VersionCheck.getServerVersion();
		} catch (IOException e1) {
		}
		Boolean isOutdated = false;
		try {
			isOutdated = VersionCheck.isOutdated();
		} catch (Exception e1) {
		}
		
		frmWotModsUpdater = new JFrame();
		frmWotModsUpdater.setTitle("Wot Mods Updater");
		frmWotModsUpdater.setBounds(100, 100, 800, 600);
		frmWotModsUpdater.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmWotModsUpdater.getContentPane().setLayout(null);
		JButton btnUpdate = new JButton("Update");
		progressBar.setForeground(Color.GREEN);
		
		
		btnUpdate.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				progressBar.setVisible(true);
				updateClick();
			}
		});
		btnUpdate.setBounds(512, 484, 89, 23);
		frmWotModsUpdater.getContentPane().add(btnUpdate);
		if ( ! isOutdated){
			btnUpdate.setText("Start");
		}
		
		
		JLabel lblVersion = new JLabel("Ihre Version: " +VersionCheck.getVersion());
		lblVersion.setFont(new Font("Times New Roman", Font.BOLD, 14));
		lblVersion.setBounds(596, 70, 178, 44);
		frmWotModsUpdater.getContentPane().add(lblVersion);
		
		JLabel lblNewLabel = new JLabel("Bild");
		lblNewLabel.setIcon(new ImageIcon("Background.jpg"));
		lblNewLabel.setBounds(0, 0, 583, 427);
		frmWotModsUpdater.getContentPane().add(lblNewLabel);
		
		JButton btnBrowse = new JButton("Browse");
		btnBrowse.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				browseCklick();
			}
		});
		btnBrowse.setBounds(394, 439, 89, 23);
		frmWotModsUpdater.getContentPane().add(btnBrowse);
		progressBar.setBounds(493, 536, 281, 14);
        frmWotModsUpdater.getContentPane().add(progressBar);
        progressBar.setIndeterminate(true);
        progressBar.setVisible(false);

				
		txtpn = new JTextPane();
		txtpn.setFont(new Font("Times New Roman", Font.BOLD, 18));
		if (Path.loadFile() != null){
			txtpn.setText("Ihr WoT Pfad lautet: "+Path.loadFile());
		} else {
			txtpn.setText("Bitte geben Sie den Pfad zu ihrem WoT an.");
		}
		txtpn.setEditable(false);
		txtpn.setBounds(10, 439, 374, 111);
		frmWotModsUpdater.getContentPane().add(txtpn);
		
		JLabel lblNewLabel_1 = new JLabel("Aktuelle Version: " +serverVersion);
		lblNewLabel_1.setFont(new Font("Times New Roman", Font.BOLD, 14));
		lblNewLabel_1.setBounds(596, 25, 178, 34);
		frmWotModsUpdater.getContentPane().add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("");
		lblNewLabel_2.setForeground(Color.BLUE);
		lblNewLabel_2.setFont(new Font("Times New Roman", Font.BOLD, 14));
		lblNewLabel_2.setBounds(596, 125, 178, 44);
			if (isOutdated){ lblNewLabel_2.setText("Ein Update ist verfügbar.");
			lblNewLabel_2.setForeground(Color.RED);
			}
			else lblNewLabel_2.setText("Ihre Version ist Aktuell.");
		frmWotModsUpdater.getContentPane().add(lblNewLabel_2);
		
		JButton btnNewButton = new JButton("Cancel");
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				System.exit(0);							
			}
		});
		btnNewButton.setBounds(648, 484, 89, 23);
		frmWotModsUpdater.getContentPane().add(btnNewButton);
		
				
	}
	
	private void updateClick() {
		Boolean isOutdated = false;
		try {
			isOutdated = VersionCheck.isOutdated();
		} catch (Exception e1) {
		}
		
		
		if (Path.loadFile() != null) {
			
			if (isOutdated){
			
				int reply = JOptionPane.showConfirmDialog(null, "Wollen Sie das Update durchführen?", "", JOptionPane.YES_NO_OPTION);
				if (reply == JOptionPane.YES_OPTION) {
					new Update("update").start();
																		
				}
				else {
					JOptionPane.showMessageDialog(null, "GOODBYE");
	           
					}
			
			}
			
			else {
				
				int reply = JOptionPane.showConfirmDialog(null, "Wollen Sie WOT starten?", "", JOptionPane.YES_NO_OPTION);
				if (reply == JOptionPane.YES_OPTION) {
					try {
						WotStart.wotStart();
					} catch (IOException e) {
					}					
				
				
				
				}
				else {
					JOptionPane.showMessageDialog(null, "GOODBYE");
                                        System.exit(0);
					}
			}
		}
		
		else {
			JOptionPane.showMessageDialog(null, "Sie haben noch keinen Pfad angeben!");
		}
	}
	
	private void browseCklick(){

		Filechooser file = new Filechooser();
		File directory = file.getDirectory();
		if (directory!= null){
			txtpn.setText(directory.toString());
			Path.createFile(directory);
		}
		
						
	}
}
